package com.rpframework.module.common.bottom.controller;

import com.rpframework.core.json.JsonResp;
import com.rpframework.module.common.bottom.api.baidu.com.JsonMap;
import com.rpframework.module.common.bottom.api.baidu.com.Map;
import com.rpframework.module.common.bottom.api.juhe.cn.Position;
import com.rpframework.module.common.bottom.entity.RegionCity;
import com.rpframework.module.common.bottom.entity.RegionCounty;
import com.rpframework.module.common.bottom.entity.RegionProvince;
import com.rpframework.module.common.bottom.entity.po.Geography;
import com.rpframework.module.common.bottom.service.RegionCityService;
import com.rpframework.module.common.bottom.service.RegionCountyService;
import com.rpframework.module.common.bottom.service.RegionProvinceService;
import com.rpframework.module.common.url.RequestDescription;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping
@RequestDescription("地区")
@ResponseBody
public class RegionController {
    private final RegionProvinceService regionProvinceService;
    private final RegionCountyService regionCountyService;
    private final RegionCityService regionCityService;

    @Autowired
    public RegionController(RegionProvinceService regionProvinceService, RegionCountyService regionCountyService, RegionCityService regionCityService) {
        this.regionProvinceService = regionProvinceService;
        this.regionCountyService = regionCountyService;
        this.regionCityService = regionCityService;
    }

    /**
     * @return 级联关系
     */
    @RequestMapping
    @RequestDescription("查询所有省及关联的市区县")
    public JsonResp<List<RegionProvince>> selectDetails() {
        return JsonResp.list(regionProvinceService.selectLinkDetails());
    }

    /**
     * @param code 区域ID
     */
    @RequestMapping
    @RequestDescription("根据区code主键查找省市")
    public JsonResp<RegionCounty> selectByCountyCode(@RequestParam Integer code) {
        return new JsonResp<RegionCounty>().data(regionCountyService.selectByCountyCode(code));
    }

    /**
     * @param geography 经纬度
     */
    @RequestMapping
    @RequestDescription("传入经纬度获取所在，省份，城市，区域多个")
    public JsonResp<RegionCity> getRegionByGeography(Geography geography) {
        //进度经纬度获取区县
        Position position = Map.getPosition(geography);
        JsonMap.check(position);
        String city = position.getAddressComponent().getCity();
        RegionCity regionCity = new RegionCity();
        regionCity.setName(city);
        regionCity = regionCityService.selectOne(regionCity);
        regionCity.setPosition(position);
        //省份
        regionCity.setRegionProvince(regionProvinceService.selectByPrimaryKey(regionCity.getCountryCode()));
        //区县
        RegionCounty regionCounty = new RegionCounty();
        regionCounty.setProvinceCode(regionCity.getCode());
        regionCity.setRegionCountyList(regionCountyService.select(regionCounty));
        return JsonResp.ok(regionCity);
    }

    @RequestMapping
    @RequestDescription("传入城市code获取所在，省份，城市，区域多个")
    public JsonResp<RegionCity> getRegionByCityCode(RegionCity regionCity) {
        regionCity.setCode(regionCity.getCode());
        regionCity = regionCityService.selectOne(regionCity);
        //省份
        regionCity.setRegionProvince(regionProvinceService.selectByPrimaryKey(regionCity.getCountryCode()));
        //区县
        RegionCounty regionCounty = new RegionCounty();
        regionCounty.setProvinceCode(regionCity.getCode());
        regionCity.setRegionCountyList(regionCountyService.select(regionCounty));
        return JsonResp.ok(regionCity);
    }

    @RequestMapping
    @RequestDescription("详细地址简单形式:x省x市x区")
    public JsonResp<String> selectSimpleByRegionCode(@RequestParam Integer code) {
        return JsonResp.ok("获取成功", regionCountyService.getAddress(code));
    }
}
